package com.china.school.pojo;

import com.alibaba.druid.pool.DruidDataSource;
import com.china.school.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;

@Component
public class DatabaseInfo {
     private String driverName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Resource
    private Address address;

//    @Autowired
//    @Qualifier("userDao2")
    @Resource(name ="userDao2")
    private UserDao userDao;

    private User user;



    @Autowired
    private List<UserDao> userDaoList;


    /**
     bean方法能够像注入属性一样获取参数
     可省略@Autowired
     */
    @Bean
    @Primary
    @Profile("dev")
    public DataSource dataSource(
            @Value("${jdbc.driver}")            String driverName,
            User user ,
               List<UserDao> userDaoList
    ){
        this.user = user;
        this.driverName = driverName;
        this.userDaoList = userDaoList;
        System.out.println("----------");
        return new DruidDataSource();
    }
    @Bean
    public DataSource dataSource(){
        return new DruidDataSource();

    }


    public List<UserDao> getUserDaoList() {
        return userDaoList;
    }

    public void setUserDaoList(List<UserDao> userDaoList) {
        this.userDaoList = userDaoList;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public User getUser() {
        return user;
    }

     public void setUser(User user) {
        this.user = user;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("DatabaseInfo{");
        sb.append("driverName='").append(driverName).append('\'');
        sb.append(", url='").append(url).append('\'');
        sb.append(", username='").append(username).append('\'');
        sb.append(", password='").append(password).append('\'');
        sb.append(", address=").append(address);
        sb.append(", userDao=").append(userDao);
        sb.append(", user=").append(user);
        sb.append(", userDaoList=").append(userDaoList);
        sb.append('}');
        return sb.toString();
    }
}
